# -*- coding: utf-8 -*-
import scrapy
from top100movies.items import Top100MoviesItem

class MovieSpider(scrapy.Spider):
    name = 'movie'
    allowed_domains = ['mtime.com']
    start_urls = ['http://www.mtime.com/top/movie/top100/']

    def parse(self, response):
        for movies_info in response.xpath('//div[@class="top_list"]/ul/li'):
            item = Top100MoviesItem()

            item['movie_name'] = movies_info.xpath('./div[@class="mov_con"]/h2/a/text()').extract_first()
            item['movie_director'] = movies_info.xpath('./div[@class="mov_con"]/p[1]/a/text()').extract_first()

            point1 = movies_info.xpath('./div[@class="mov_point"]/b/span[1]/text()').extract()
            point2 = movies_info.xpath('./div[@class="mov_point"]/b/span[2]/text()').extract()
            if point1:
                if point2:
                    item['movie_point'] = point1[0] + point2[0]
            else:
                item['movie_point'] = "8.6"

            yield item

        # 待打开的所有的分页link 0-8
        new_links = ["http://www.mtime.com/top/movie/top100/index-{}.html".format(i) for i in range(2, 11)]

        # 获取当前page页数
        page =  response.xpath('//div[@class="top_list"]/ul/li/div/em/text()').extract_first()
        page = int(page) // 10 + 1

        if page < 10:
            new_link = new_links[page-1]
            yield scrapy.Request(new_link, callback=self.parse)